package com.zmc.order.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.order.infrastructure.bean.query.CommentMemberInfoQuery;
import com.zmc.order.infrastructure.enums.CommentEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.order.infrastructure.dao.CommentMemberInfoDao;
import com.zmc.order.infrastructure.entity.CommentMemberInfoEntity;
import com.zmc.order.infrastructure.service.CommentMemberInfoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commentMemberInfoService")
public class CommentMemberInfoServiceImpl extends ServiceImpl<CommentMemberInfoDao, CommentMemberInfoEntity> implements CommentMemberInfoService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public Page<CommentMemberInfoEntity> queryPage(CommentMemberInfoQuery query) {
        return baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
    }

    @Override
    public CommentMemberInfoEntity getBySpuIdAndOrderSn(String spuId, String orderSn) {
        LambdaQueryWrapper<CommentMemberInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentMemberInfoEntity::getSpuId, spuId)
                .eq(CommentMemberInfoEntity::getOrderSn, orderSn)
                .last(" limit 1");
        return getOne(wrapper);
    }

    @Override
    public void updateByMemberIdAndOrderSn(String memberId, String orderSn, int status) {
        LambdaUpdateWrapper<CommentMemberInfoEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CommentMemberInfoEntity::getMemberId, memberId)
                .eq(CommentMemberInfoEntity::getOrderSn, orderSn)
                .set(CommentMemberInfoEntity::getCommentStatus, status);
        update(wrapper);
    }

    @Override
    @Transactional
    public void initCommentMemberInfo(String memberId, String orderSn, String spuId) {
        CommentMemberInfoEntity entity = new CommentMemberInfoEntity();
        entity.setMemberId(memberId);
        entity.setSpuId(spuId);
        entity.setOrderSn(orderSn);
        entity.setCommentStatus(CommentEnum.COMMENT_TYPE_FIRST.getStatus());
        save(entity);
    }

    private LambdaQueryWrapper<CommentMemberInfoEntity> getWrapper(CommentMemberInfoQuery query) {
        LambdaQueryWrapper<CommentMemberInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentMemberInfoEntity::getMemberId, authTokenService.getMemberInfo().getMemberId())
                .eq(CommentMemberInfoEntity::getCommentStatus, query.getStatus());
        return wrapper;
    }

}